/**
 * DMPuzzile
 * PuzzileActivity.java
 * com.dm.puzzile
 * 
 */
package com.dm.puzzile;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.dm.DMButton;
import com.dm.DMListActivity;
import com.dm.DMUtil;
import com.dmx.lightd.R;

/*********************************************************
 * @author : zhaohua
 * @version : 2014-1-1
 * @see : 
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class PuzzileActivity extends DMListActivity implements OnItemClickListener
{

    public static enum TYPE{
        TOP,BOTTOM,LEFT,RIGHT,PRE,BACK,MAIN,MORE
    }
    
    private ArrayAdapter<String> mAdpater;
    private static boolean isFirst = true;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentRId());
        getListView().setOnItemClickListener(this);
        initAds();
    }
    
    private void initAds() {
        if(DMUtil.isRunning) {
            bindAd(R.id.btn_top_00, 0);
            bindAd(R.id.btn_bottom_01, 1);
            bindAd(R.id.btn_left_02, 1);
            bindAd(R.id.btn_right_03, 0);
            bindAd(R.id.btn_pre_04, 1);
            bindAd(R.id.btn_back_05, 0);
            bindAd(R.id.btn_more_00, 1);
            bindAd(R.id.btn_search_back, 0);
            bindAd(R.id.btn_top_0, 0);
            bindAd(R.id.btn_bottom_1, 1);
            bindAd(R.id.btn_left_2, 1);
            bindAd(R.id.btn_right_3, 0);
            bindAd(R.id.btn_pre_4, 1);
            bindAd(R.id.btn_back_5, 0);
            bindAd(R.id.btn_more_0, 1);
            bindAd(R.id.btn_search_top, 0);
        }                
    }
    
    private void bindAd(int btnid, int adId) {
        View btn = findViewById(btnid);
        if(btn != null && btn instanceof DMButton) {
            DMButton view = (DMButton)btn;
            view.bindAdView(getAdView(adId));
        }
    }
    
    protected TYPE getType() {
        return TYPE.MAIN;
    }
    
    protected int getContentRId() {
        switch(getType()) {
        case TOP:
            return R.layout.activity_top;
        case BOTTOM:
            return R.layout.activity_bottom;
        case LEFT:
            return R.layout.activity_left;
        case RIGHT:
            return R.layout.activity_right;
        case PRE:
            return R.layout.activity_pre;
        case BACK:
            return R.layout.activity_back;
        case MAIN:
            return R.layout.activity_main;
        case MORE:
            return R.layout.activity_more;
        }
        return R.layout.activity_main;
    }
    
    protected int getListViewRId() {
        switch(getType()) {
        case TOP:
            return R.id.list_top;
        case BOTTOM:
            return R.id.list_bottom;
        case LEFT:
            return R.id.list_left;
        case RIGHT:
            return R.id.list_right;
        case PRE:
            return R.id.list_pre;
        case BACK:
            return R.id.list_back;
        case MAIN:
            return R.id.list_main;
        case MORE:
            return R.id.list_more;
        }
        return R.id.list_main;
    }
    
    protected ListView getListView() {
        return (ListView)findViewById(getListViewRId());
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        if(mAdpater == null) {
            showLoadingDalay();
            long seed = getIntent().getLongExtra("SEED", System.currentTimeMillis());
            mAdpater = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getData(seed));
            getListView().setAdapter(mAdpater);
        }
    }
    
    public void startThisActivty(long seed) {
        
    }
    
    private List<String> getData(long seed){
        Random dom = new Random(seed); 
        int size = Math.abs((int)(seed % 4));
        if(isFirst) {
            size = 4;
            isFirst = false;
        }else {
            size = size == 0 ? 2 : size < 2 ? 2 : size;
        }
        List<String> list = new ArrayList<String>(size);
        for(int i = 0; i < size ; i++) {
            list.add(dom.nextLong() + "");
        }
        return list;
    }
    
    public void onClickTop(View v) {
        TopActivity.startActivty(this, System.currentTimeMillis());
    }
    
    public void onClickBottom(View v) {
        BottomActivity.startActivty(this, System.currentTimeMillis());
    }
    
    public void onClickLeft(View v) {
        LeftActivity.startActivty(this, System.currentTimeMillis());
    }
    
    public void onClickRight(View v) {
        RightActivity.startActivty(this, System.currentTimeMillis());
    }
    
    public void onClickPre(View v) {
        PreActivity.startActivty(this, System.currentTimeMillis());
    }
    
    public void onClickBack(View v) {
        BackActivity.startActivty(this, System.currentTimeMillis());
    }
    
    public void onClickMore(View v) {
        MoreActivity.startActivty(this, System.currentTimeMillis());
    }
    
    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long arg3)
    {
        String seed = (String)adapter.getItemAtPosition(position);
        if(seed != null) {        
           mAdpater.clear();
           List<String> list = getData(Long.parseLong(seed));
           for(String one : list) {
               mAdpater.add(one);
           }
        }
    }

    
   public static int getTemplateRid() {
       return R.layout.activity_template;
   }
   
   public static int getTopBarLayRid() {
       return R.id.topBarLay;
   }
   
   public static int getBtmBarLayRid() {
       return R.id.btmBarLay;
   }
   
   public static int getContainerLayRid() {
       return R.id.containerLay;
   }
}
